#include <stdio.h>

#define MAX_WIDTH 1000

int
str_reverse(char *s)
{
	
	int i, len, swap;

	len = strlen(s);
	for(i = 0; i < len / 2; i++) {
		swap = s[i];
		s[i] = s[len - 1 - i];
		s[len - 1 - i] =  swap;
	}
}

/*
function: string add function
parameters:
	a   string operator
	b   string operator
	r   result buffer
	len result buffer length
*/

int 
str_add(const char *a, const char *b, char *r, int len)
{
	size_t x, y;
	int cb;
	
	char p, q;
	int i;

	char buf[MAX_WIDTH] = {0};

	x = strlen(a);
	y = strlen(b);

	i = cb = 0;
	while((x != 0) || (y != 0)) {
		if (i > len)
			return -1;
		if (x > 0) {
			p = a[x - 1] - '0';
			x--;
		} else
			p = 0;
		if (y > 0) {
			q = b[y - 1] - '0';
			y--;
		} else
			q = 0;
		buf[i++] = (cb + p + q) % 10 + '0';
		if ((cb + p + q) >= 10)
			cb = 1;
		else
			cb = 0;
	}
	if (cb)
		buf[i++] = '1';
	buf[i] = '\0';
	str_reverse(buf);
	strcpy(r, buf);

	return 0;
}


/*
function: string multiply function
parameters:
	a   string operator
	b   string operator
	r   result buffer
	len result buffer length
*/
int
str_multiply(const char *a, const char *b, char *r, int len)
{
	int i, ret;

	i = atoi(a);
	strcpy(r, b);
	printf("r=%s\n", r);
	while (--i) {
		ret = str_add(r, b, r, len);
		if (ret != 0)
			return ret;
	}
	printf("%s*%s=%s\n", a, b, r);
}

int
main()
{
	char buf[MAX_WIDTH];

	str_add("6434343534", "694032472304732042730434378437429472944", buf, 100); 
	str_multiply("43", "69090898083472184927309438744729483748947329842749834789472894328947327892348923498242794324893298423", buf, MAX_WIDTH);

	return 0;
}
